package org.feng.controller;

import cn.dev33.satoken.secure.SaBase64Util;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.feng.base.constant.RedisConstant;
import org.feng.base.entity.Result;
import org.feng.entity.User;
import org.feng.service.UserService;
import org.feng.utils.ExcelUtils;
import org.feng.utils.UserUtil;
import org.feng.utils.condition.Condition;
import org.feng.utils.redis.RedisString;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author feng
 * @since 2021-08-27
 */
@RestController
@RequestMapping("/system/user")
@AllArgsConstructor
public class UserController {

    private final UserService userService;
    private final RedisString redisString;


    /**
     * 导出
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        ExcelUtils.writeExcel(response, userService.list(), User.class);
    }

    /**
     * 根据部门id获取下边所有人员
     */
    @GetMapping("/deptUser")
    public Result<IPage<User>> deptUser(User user, @RequestParam Long deptId) {
        user.setDeptId(null);
        return Result.success(userService.deptUser(user, deptId));
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/userInfo")
    public Result<User> userInfo() {
        return Result.success(userService.getById(StpUtil.getLoginIdAsLong()));
    }

    /**
     * 修改用户信息
     */
    @PutMapping("/updateUserInfo")
    public Result<Object> updateUserInfo(@RequestBody User user) {
        user.setId(UserUtil.getUserId());
        return Result.success(userService.updateById(user));
    }

    /**
     * 修改密码
     */
    @PutMapping("/restPassword")
    public Result<Object> restPassword(@RequestParam String oldPassword, @RequestParam String newPassword) {
        User user = userService.getById(UserUtil.getUserId());
        if (!SaBase64Util.encode(oldPassword).equals(user.getPassword())) {
            return Result.error("密码输入错误");
        } else {
            user.setPassword(SaBase64Util.encode(newPassword));
            return Result.success(userService.updateById(user));
        }
    }

    /**
     * 列表
     */
    @GetMapping
    public Result<List<User>> list(User user) {
        return Result.success(userService.list(Condition.getQueryWrapper(user)));
    }

    /**
     * 分页列表
     */
    @GetMapping("/page")
    public Result<IPage<User>> page(User user) {
        return Result.success(userService.page(Condition.getPage(), Condition.getQueryWrapper(user)));
    }

    /**
     * 详情
     */
    @GetMapping("/query")
    public Result<User> detail(@RequestParam Long id) {
        return Result.success(userService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping
    public Result<Boolean> save(@RequestBody User user) {
        long count = userService.count(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()));
        if (count > 0) {
            return Result.error("账户已存在,请重新输入账号");
        }
        user.setPassword(SaBase64Util.encode("123456"));
        return Result.status(userService.save(user));
    }

    /**
     * 修改
     */
    @PutMapping
    public Result<Boolean> update(@RequestBody User user) {
        long count = userService.count(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()));
        if (count > 0) {
            return Result.error("账户已存在,请重新输入账号");
        }
        redisString.set(RedisConstant.USER_INFO_PREFIX + user.getId(), JSON.toJSONString(user));
        return Result.status(userService.updateById(user));
    }

    /**
     * 删除
     */
    @DeleteMapping
    public Result<Boolean> delete(@RequestParam String ids) {
        return Result.status(userService.removeByIds(Arrays.asList(ids.split(","))));
    }
}

